package servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.ActorJob;
import domain.CrewJob;
import domain.User;
import ex.DatabaseException;

import service.JobService;
import service.UserService;
import service.WorkService;
import util.RegexUtil;

public class ApplyToJob extends HttpServlet {

	JobService jservice = new JobService();
	WorkService wservice = new WorkService();
	UserService uservice=new UserService();
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		try {
			if (session.getAttribute("user") != null) {
				domain.User user = (domain.User) session.getAttribute("user");
				if (request.getParameter("action") == null) {
					if (request.getParameter("toa") != null) {
						String sajid = request.getParameter("toa");
						if (RegexUtil.isParsableToInt(sajid)) {
							int ajid = Integer.parseInt(sajid);
							ActorJob ajob = jservice.getActorJobByAjid(ajid);
							
							if (ajob != null ){
								wservice.addActed(user, ajob);
							}

							response.sendRedirect("movie?which="+ajob.getMid());
						} else {
							response.sendRedirect("home");
						}
					} else if (request.getParameter("toc") != null) {
						String scijd = request.getParameter("toc");
						if (RegexUtil.isParsableToInt(scijd)) {
							int cijd = Integer.parseInt(scijd);
							CrewJob cjob = jservice.getCrewJobByCjid(cijd);
							if (cjob != null)
							{
								wservice.addWorkedAsCrew(user, cjob);
							}

							response.sendRedirect("movie?which="+cjob.getMid());

						} else {
							response.sendRedirect("home");
						}
					} else {
						response.sendRedirect("home");
					}
				} 
				else
				{
					String uid = request.getParameter("uid");
					if (RegexUtil.isParsableToInt(uid)) {
						User u=uservice.getUserByUid(Integer.parseInt(uid));
						if(u!=null){
							if (request.getParameter("action").equalsIgnoreCase(
									"accept")) {
								String aijd=request.getParameter("ajid");
								String cijd=request.getParameter("cjid");
								if (RegexUtil.isParsableToInt(aijd)) {
									ActorJob aj=jservice.getActorJobByAjid(Integer.parseInt(aijd));
									if(aj!=null && aj.getCount()>0)
									{
										wservice.acceptApply(u,aj);
										int cc=aj.getCount();
										cc--;
										aj.setCount(cc);
										jservice.updateActorJob(aj);
										response.sendRedirect("movie?which="+aj.getMid());
									}									
								}
								else if (RegexUtil.isParsableToInt(cijd)) {
									CrewJob cj=jservice.getCrewJobByCjid(Integer.parseInt(cijd));
									if(cj!=null && cj.getCount()>0){
										wservice.acceptApply(u,cj);
										int cc=cj.getCount();
										cc--;
										cj.setCount(cc);
										jservice.updateCrewJob(cj);
										response.sendRedirect("movie?which="+cj.getMid());
									}
								}								
							
							}
							else if (request.getParameter("action").equalsIgnoreCase(
									"reject")) {
								String aijd=request.getParameter("ajid");
								String cijd=request.getParameter("cjid");
								if (RegexUtil.isParsableToInt(aijd)) {
									ActorJob aj=jservice.getActorJobByAjid(Integer.parseInt(aijd));
									if(aj!=null){
										Map<User,Integer> acteds=wservice.getActedUsersByAjid(aj.getAjid());
										if(acteds.containsKey(u)){
											if(acteds.get(u)==1){
												int cc=aj.getCount();
												cc++;
												aj.setCount(cc);
												jservice.updateActorJob(aj);
												
											}
										}
										wservice.rejectApply(u,aj);
									}
									response.sendRedirect("movie?which="+aj.getMid());
								}
								else if (RegexUtil.isParsableToInt(cijd)) {
									CrewJob cj=jservice.getCrewJobByCjid(Integer.parseInt(cijd));
									if(cj!=null){
										Map<User,Integer> workeds=wservice.getWorkedAsCrewUsersByCjid(cj.getCjid());
										if(workeds.containsKey(u)){
											if(workeds.get(u)==1){
												int cc=cj.getCount();
												cc++;
												cj.setCount(cc);
												jservice.updateCrewJob(cj);
											}
										}
										wservice.rejectApply(u,cj);
										
									}
									response.sendRedirect("movie?which="+cj.getMid());
								}
								

							}
						}
					}
					
				}
			}
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		// TODO Auto-generated method stub
	}

}
